#include "mystatusbar.h"

#include <QString>
#include <QTimer>
#include <QPainter>
#include <QStyleOption>
#include <QStyleOptionButton>
#include <QStatusBar>
#include <QStatusTipEvent>
#include <QRect>
#include <QApplication>

#include <QPixmap>

#include <QDebug>


class MyStatusBar::MyStatusBarPrivate{
public:
	MyStatusBarPrivate();
	QRect messageRect() const;

	QString message;
	QTimer *timer;
	QStatusBar bar;

	QPixmap m_connected;
	QPixmap m_disconnected;
	QPixmap m_connecting;
	QPixmap m_encrypted;

	MyStatusBar::ConnectStatus m_status;
	QString m_statusText;
};


MyStatusBar::MyStatusBarPrivate::MyStatusBarPrivate(){
	timer = NULL;

	m_statusText = tr("Nie połączono");
	m_status = MyStatusBar::Disconnected;

	m_connected.load( ":/res/quickopen.png" );
	m_disconnected.load( ":/res/edit-delete.png" );
	m_connecting.load( ":/res/hourglass.png" );
	m_encrypted.load( ":/res/object-locked.png" );
}

QRect MyStatusBar::MyStatusBarPrivate::messageRect() const {
	QRect tmp;
	tmp.setTop(0);
	tmp.setLeft(6 + 16);
	tmp.setWidth(480);
	tmp.setHeight(24);

	return tmp;
}

//end of d-class
//------------------------------------------------------------------------------
MyStatusBar::MyStatusBar(QWidget *parent) :
	QFrame(parent),
	d(new MyStatusBarPrivate())
{
	setMouseTracking( true );

}

QSize MyStatusBar::sizeHint() const {
	QSize size = QFrame::sizeHint();
	size.setHeight(24);
	return size;
}

void MyStatusBar::paintEvent(QPaintEvent *event){
	QFrame::paintEvent(event);

	bool haveMessage = !d->message.isEmpty();

	QPainter p(this);
	QStyleOption opt;
	opt.initFrom(&d->bar);
	style()->drawPrimitive(QStyle::PE_PanelStatusBar, &opt, &p, this);


	QRect connectionState;
	connectionState.setLeft(3);

	switch( d->m_status ){
	case Disconnected:
		connectionState.setTop( (24 - d->m_disconnected.height()) / 2 );
		connectionState.setSize( d->m_disconnected.size() );
		p.drawPixmap( connectionState, d->m_disconnected );
		break;
	case Connected:
		connectionState.setTop( (24 - d->m_connected.height()) / 2 );
		connectionState.setSize( d->m_connected.size() );
		p.drawPixmap( connectionState, d->m_connected );
		break;
	case Connecting:
		connectionState.setTop( (24 - d->m_connecting.height()) / 2 );
		connectionState.setSize( d->m_connecting.size() );
		p.drawPixmap( connectionState, d->m_connecting );
		break;
	case Encrypted:
		connectionState.setTop( (24 - d->m_encrypted.height()) / 2 );
		connectionState.setSize( d->m_encrypted.size() );
		p.drawPixmap( connectionState, d->m_encrypted );
	}

	if( haveMessage ){
		p.setPen(palette().foreground().color());
		p.drawText( d->messageRect(),
					Qt::AlignLeading | Qt::AlignVCenter | Qt::TextSingleLine,
					d->message );
	}
}

bool MyStatusBar::eventFilter(QObject *obj, QEvent *e){
	if( e->type() == QEvent::StatusTip ){
		QStatusTipEvent *event = dynamic_cast<QStatusTipEvent*>(e);
		showMessage( event->tip() );
		return true;
	}

	// standard event processing
	return QFrame::eventFilter(obj, e);
}

bool MyStatusBar::event(QEvent *e){
	if( e->type() == QEvent::MouseMove ){
		QMouseEvent *event = dynamic_cast<QMouseEvent*>(e);
		//qDebug() << event->pos();
		if( event->x() >= 3 && event->x() <= 3 + 16 &&
			event->y() >= (24-16)/2 && event->y() <= (24-16)/2 + 16 )
		{
			showMessage( d->m_statusText );
		}else{
			showMessage("");
		}
	}


	if( e->type() == QEvent::MouseButtonRelease ){
		QMouseEvent *event = dynamic_cast<QMouseEvent*>(e);
		if( event->x() >= 3 && event->x() <= 3 + 16 &&
			event->y() >= (24-16)/2 && event->y() <= (24-16)/2 + 16 )
		{
			if( event->button() == Qt::LeftButton )
				emit clicked();
			else if( event->button() == Qt::RightButton )
				emit clickedRight();
		}
	}

	if( e->type() == QEvent::Leave ){
		showMessage("");
	}

	return QFrame::event(e);
}

QString MyStatusBar::currentMessage() const {
	return d->message;
}


void MyStatusBar::showMessage(const QString &text, int timeout){
	d->message = text;

	if( timeout > 0 ){
		if( !d->timer ){
			d->timer = new QTimer(this);
			d->timer->setSingleShot( true );
			connect(d->timer, SIGNAL(timeout()), this, SLOT(clearMessage()));
		}
		d->timer->start( timeout );
	}else if( d->timer){
		delete d->timer;
		d->timer = 0;
	}

	repaint( d->messageRect() );
}

void MyStatusBar::clearMessage(){
	if( d->message.isEmpty() )
		return;

	if( d->timer ){
		d->timer->stop();
		delete d->timer;
		d->timer = 0;
	}
	d->message.clear();

	repaint( d->messageRect() );
}

void MyStatusBar::setConnectionStatusText(const QString &status){
	d->m_statusText = status;
}

void MyStatusBar::setConnectionStatus(const ConnectStatus &status){
	d->m_status = status;
	repaint();
}

QString MyStatusBar::getConnectionStatusText() const {
	return d->m_statusText;
}

MyStatusBar::ConnectStatus MyStatusBar::getConnectionStatus() const {
	return d->m_status;
}
